package br.com.sguc.util;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailSender extends Authenticator {
	private String usuario;
	private String senha;
	private List<String> destinatarios;
	private String remetente;
	private String portaNaoSegura;
	private String portaSegura;
	private String host;
	private String assunto;
	private String corpoDaMensagem;
	private boolean autenticar;
	private boolean debugar;
	private Multipart mimeMultiParte;

	public EmailSender() {
		this.host = "smtp.gmail.com";
		this.portaNaoSegura = "465";
		this.portaSegura = "465";
		this.usuario = "";
		this.senha = "";
		this.remetente = "";
		this.assunto = "";
		this.corpoDaMensagem = "";
		this.debugar = false;
		this.autenticar = true;
		this.mimeMultiParte = new MimeMultipart();

		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
		CommandMap.setDefaultCommandMap(mc);
	}

	public EmailSender(String user, String pass) {
		this();
		this.usuario = user;
		this.senha = pass;
	}

	public boolean send() throws Exception {
		Properties props = _setProperties();
		if (!this.usuario.equals("") && !this.senha.equals("") && this.destinatarios.size() > 0 && !this.remetente.equals("") && !this.assunto.equals("") && !this.corpoDaMensagem.equals("")) {
			Session session = Session.getInstance(props, this);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(this.remetente));
			InternetAddress[] addressTo = new InternetAddress[this.destinatarios.size()];
			for (int i = 0; i < this.destinatarios.size(); i++) {
				addressTo[i] = new InternetAddress(this.destinatarios.get(i));
			}
			msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
			msg.setSubject(this.assunto);
			msg.setSentDate(new Date());

			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(this.corpoDaMensagem);
			this.mimeMultiParte.addBodyPart(messageBodyPart);

			msg.setContent(this.mimeMultiParte);

			Transport.send(msg);
			return true;
		} else {
			return false;
		}
	}

	public void addAttachment(String filename) throws Exception {
		BodyPart messageBodyPart = new MimeBodyPart();
		DataSource source = new FileDataSource(filename);
		messageBodyPart.setDataHandler(new DataHandler(source));
		messageBodyPart.setFileName(filename);
		this.mimeMultiParte.addBodyPart(messageBodyPart);
	}

	@Override
	public javax.mail.PasswordAuthentication getPasswordAuthentication() {
		return new javax.mail.PasswordAuthentication(this.usuario, this.senha);
	}

	private Properties _setProperties() {
		Properties props = new Properties();
		props.put("mail.smtp.host", this.host);
		if (this.debugar) {
			props.put("mail.debug", "true");
		}
		if (this.autenticar) {
			props.put("mail.smtp.auth", "true");
		}
		props.put("mail.smtp.port", this.portaNaoSegura);
		props.put("mail.smtp.socketFactory.port", this.portaSegura);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		return props;
	}

	public List<String> getDestinatarios() {
		return this.destinatarios;
	}

	public void setDestinatarios(List<String> destinatarios) {
		this.destinatarios = destinatarios;
	}

	public String getRemetente() {
		return this.remetente;
	}

	public void setRemetente(String remetente) {
		this.remetente = remetente;
	}

	public String getAssunto() {
		return this.assunto;
	}

	public void setAssunto(String assunto) {
		this.assunto = assunto;
	}

	public String getCorpoDaMensagem() {
		return this.corpoDaMensagem;
	}

	public void setCorpoDaMensagem(String corpoDaMensagem) {
		this.corpoDaMensagem = corpoDaMensagem;
	}
}